﻿
using Authorization.Entitys.DTO;
using Authorization.Entitys.Model;
using Authorization.Infrastructure;
using Authorization.IRepository;
using Microsoft.AspNetCore.Identity;
using System;
using System.Threading.Tasks;

namespace Authorization.Application
{
    public class UserApps
    {
        private readonly IUserRepository userRepository;

        private readonly IRepository.IUnitOfWork unitOfWork;
        private readonly UserManager<AuthorizationUser> userManager;


        public UserApps(IUserRepository _userRepository, IRepository.IUnitOfWork _unitOfWork, UserManager<AuthorizationUser> _userManager)
        {
            userRepository = _userRepository;
            unitOfWork = _unitOfWork;
            userManager = _userManager;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<NarutoResult> UpdatePassword(string userId, UpdatePasswordDTO info)
        {
            if (userId != info.userId)
            {
                return new NarutoFailResult("用户信息错误");
            }
            if (string.IsNullOrWhiteSpace(info.newPswd))
            {
                return new NarutoFailResult("请输入新密码");
            }
            //更新密码
            await userRepository.UpdatePasswordByUserId(userId, info.newPswd);

            await unitOfWork.SaveChangeAsync();

            return new NarutoSuccessResult("操作成功");
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<NarutoResult> Register(RegisterDTO info)
        {
            if (string.IsNullOrWhiteSpace(info.userName))
            {
                return new NarutoFailResult("请输入用户名");
            }
            if (string.IsNullOrWhiteSpace(info.name))
            {
                return new NarutoFailResult("请输入姓名");
            }
            if (string.IsNullOrWhiteSpace(info.password))
            {
                return new NarutoFailResult("请输入密码");
            }
            if (string.IsNullOrWhiteSpace(info.email))
            {
                return new NarutoFailResult("请输入邮箱地址");
            }
            //验证用户名是否存在
            if (await userRepository.ExistsUserName(info.userName))
            {
                return new NarutoFailResult("当前账号已经存在");
            }
            //验证邮箱是否存在
            if (await userRepository.ExistsEmail(info.email))
            {
                return new NarutoFailResult("当前邮箱已经存在");
            }
            //新增用户数据
            await userRepository.Add(new Entitys.Models.User
            {
                Id = SnowFlakeHelper.NewID(),
                CreateTime = DateTime.Now,
                Email = info.email,
                Name = info.name,
                HeardImgUrl = "",
                Mobile = "",
                OrganizationId = SnowFlakeHelper.NewID(),
                Password = info.password,
                UserName = info.userName,
                Vaild = 1
            });
            await unitOfWork.SaveChangeAsync();
            return new NarutoSuccessResult("操作成功");
        }
        /// <summary>
        /// 根据邮箱重置密码
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<NarutoResult> ResetPswdByEmail(ResetPswdByEmailDTO info)
        {
            if (string.IsNullOrWhiteSpace(info.pswd))
            {
                return new NarutoFailResult("请输入密码");
            }
            if (string.IsNullOrWhiteSpace(info.email))
            {
                return new NarutoFailResult("请输入邮箱地址");
            }
            //验证邮箱是否存在
            if (!await userRepository.ExistsEmail(info.email))
            {
                return new NarutoFailResult("当前邮箱不存在");
            }

            //更新密码
            await userRepository.UpdatePasswordByEmail(info.email, info.pswd);

            await unitOfWork.SaveChangeAsync();

            return new NarutoSuccessResult("操作成功");
        }
    }
}
